<?php

class Auth extends Controller
{
	const DEFAULT_CATEGORY = 'console';
	/*
	*	Construct
	*	@PARA: null
	*	@RETURN: null
	*/
	public function __construct()
	{
		parent::Controller();
	}
	
	/*
	*	login
	*	@PARA: null
	*	@RETURN: null
	*/
	public function login()
	{
		$data['menuView']	= $this->_getMenuView();
		
		$this->load->helper('url');
		$submit = $this->input->post('submit');
		if($submit)
		{
			$data['productionView'] = $this->_checkUser();			
		}		
		else
		{
			$data['productionView'] = $this->load->view('auth/login_form', $data, true);			
		}
		
		$this->load->library('layout');
		$this->layout->render_page('homepage/main_page', $data);
	}
	
	/*
	* 	register
	*	@PARA: null
	*	@RETURN: null
	*/
	public function register()
	{
		$data['menuView']	= $this->_getMenuView();
		
		$submit = $this->input->post('submit');
		if($submit)
		{//register a new user
			$inputData = $this->_getFormValues();
			$this->load->model('users_model','usersModel');
			$this->usersModel->addUser($inputData);		
			$data['productionView'] = $this->load->view('auth/register_confirm', '', true);	
		}		
		else
		{//show the registration form
			$this->load->model('titles_model','titlesModel');
			$data['titlesList'] = $this->titlesModel->retrieveOptions();
			$data['productionView'] = $this->load->view('auth/register_form', $data, true);			
		}
		$this->load->library('layout');
		$this->layout->render_page('homepage/main_page', $data);				
	}

	/*
	* 	register
	*	@PARA: null
	*	@RETURN: null
	*/	
	public function logout()
	{
		$this->session->sess_destroy();
		$this->layout->render_page('auth/logout','');
	}
	
	/*
	* check the user's requeste for login
	* @PARA: $_POST
	* @ACCESS: private
	* @RETURN: NULL	
	*/	
	private function _checkUser()
	{
		$loginError['errors'] = array();
		
		$userName =  $this->input->post('user_name', TRUE);
		$password =  $this->input->post('password', TRUE);
		
		if(empty($userName) || empty($password))
		{
			$loginError['errors'][] = "User name and password can not be empty.";			
		}	
		else
		{
			$loginDetails = array("userName"=>'',"password"=>''); 
			if (ctype_alpha($userName)) 
			{ 
				$loginDetails['userName'] = $userName; 
			} 
			if (ctype_alnum($password)) 
			{ 
				$loginDetails['password'] = md5($password); 
			} 

			$this->load->model('users_model', 'usersModel');                  // Instantiate the model
	        $foundUser = $this->usersModel->checkUser( $loginDetails ); 
			if($foundUser)
			{
				$this->_setUserSession($foundUser);
			}
			else
			{
				//error redirect back
				$loginError['errors'][] = "Invalid password or user name.";
			}			
		}	
		
		if(count($loginError['errors'])>0)
		{
			$loginView = $this->load->view('auth/login_form', $loginError, true);
		}
		else
		{
			$loginView = $this->load->view('auth/login_confirm', '', true);
		}
		return $loginView;
	}
	
	/*
	*	setUserSession
	*	@PARA: Array
	*	@RETURN: null
	*/
	function _setUserSession($foundUser)
	{
		$this->session->sess_destroy();
		
		$this->session->set_userdata('login_status', true);
		$this->session->set_userdata($foundUser);
	}
	
	
	/*
	*	getFormValues
	*	@PARA: null
	*	@RETURN: $data
	*	@ACCESS: private
	*/
	private function _getFormValues()
	{
		$data['id']			= $this->input->post('id', TRUE);
		$data['user_name']	= $this->input->post('user_name', TRUE);
		$data['user_passwd']= $this->input->post('password', TRUE);		
		return $data;
	}
	
	/*
	*	getMenuView - this is duplcated in games. Have to think about how to resolve that.
	*	@PARA: catgory
	*	@RETURN: $menuView
	*	@ACCESS: private
	*/
	private function _getMenuView($category=null)
	{
		$menuView= '';
		$this->load->model('menus_model', 'menusModel');
		$menuData = $this->menusModel->getMenuData();
		if($menuData)
		{
			$menuView = $this->load->view('homepage/category_menus', $menuData, true);
		}
		return $menuView;
	}	
}


?>